package com.smz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smz.dao.TReservoirDAO;
import com.smz.entity.TMenu;
import com.smz.entity.TReservoir;
import com.smz.service.TMenuService;
import com.smz.service.TReservoirService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TReservoirServiceImpl extends ServiceImpl<TReservoirDAO, TReservoir> implements TReservoirService {

    @Autowired
    private TReservoirService reservoirService;
    @Autowired
    private TMenuService tMenuService;


    public ResponseEntity findOne(Integer wid, Integer trId, String time) {
        if (time .equals("没有") ) {
            if (wid != 0 && trId != 0) {
                List<TReservoir> tReservoirById = reservoirService.list(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getWareId, wid).eq(TReservoir::getId, trId));
                for (TReservoir t : tReservoirById) {
                    TMenu byId = tMenuService.getById(t.getWareId());
                    if (byId.getStatus().equals("1")) {
                        if (t.getStatus() == 1) {
                            break;
                        } else {
                            t.setStatus(0);
                        }
                    }else{
                        t.setStatus(0);
                    }
                }
                return ResponseEntity.ok(tReservoirById);
            }
            if (wid != 0 && trId == 0) {
                List<TReservoir> tReservoirById = reservoirService.list(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getWareId, wid));
                for (TReservoir tr : tReservoirById) {
                    TMenu byId = tMenuService.getById(tr.getWareId());
                    if (byId.getStatus().equals("1")) {
                        if (tr.getStatus() == 1) {
                            System.out.println(1);
                        } else {
                            tr.setStatus(0);
                        }
                    }else{
                        tr.setStatus(0);
                    }
                }
                return ResponseEntity.ok(tReservoirById);
            }
        } else {
            if (wid != 0) {
                if (wid != 0 && trId != 0) {
                    List<TReservoir> tReservoirById = reservoirService.list(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getWareId, wid).eq(TReservoir::getId, trId).likeRight(TReservoir::getDateTime, time));
                    for (TReservoir tr : tReservoirById) {
                        TMenu byId = tMenuService.getById(tr.getWareId());
                        if (byId.getStatus().equals("1")) {
                            if (tr.getStatus() == 1) {
                                System.out.println(2);
                            } else {
                                tr.setStatus(0);
                            }
                        }else{
                            tr.setStatus(0);
                        }
                    }
                    return ResponseEntity.ok(tReservoirById);
                }
                if (wid != 0 && trId == 0) {
                    List<TReservoir> tReservoirById = reservoirService.list(new QueryWrapper<TReservoir>().lambda().eq(TReservoir::getWareId, wid).likeRight(TReservoir::getDateTime, time));
                    for (TReservoir tr : tReservoirById) {
                        TMenu byId = tMenuService.getById(tr.getWareId());
                        if (byId.getStatus().equals("1")) {
                            if (tr.getStatus() == 1) {
                                System.out.println(3);
                            } else {
                                tr.setStatus(0);
                            }
                        }else{
                            tr.setStatus(0);
                        }
                    }
                    return ResponseEntity.ok(tReservoirById);
                }
            } else {
                List<TReservoir> tReservoirById = reservoirService.list(new QueryWrapper<TReservoir>().lambda().likeRight(TReservoir::getDateTime, time));
                for (TReservoir tr : tReservoirById) {
                    TMenu byId = tMenuService.getById(tr.getWareId());
                    if (byId.getStatus().equals("1")) {
                        if (tr.getStatus() == 1) {
                            System.out.println(4);
                        } else {
                            tr.setStatus(0);
                        }
                    }else{
                        tr.setStatus(0);
                    }
                }
                return ResponseEntity.ok(tReservoirById);
            }
        }
        return null;
    }

    @Override
    public Page<List<TReservoir>> page(Integer current, Integer row) {
        Page listPage = new Page<>(current, row);
        Page page = reservoirService.page(listPage);
        page.setTotal(reservoirService.count());
        List<TReservoir> records = page.getRecords();
        for (TReservoir t : records) {
            TMenu byId = tMenuService.getById(t.getWareId());
            if (byId.getStatus().equals("1")) {
                if (t.getStatus() == 1) {
                } else {
                    t.setStatus(0);
                }
            }else{
                t.setStatus(0);
            }
        }
        return page;
    }
}
